//package com.plian.system.service.dj.impl;
//
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.plian.Tools.*;
//import com.plian.Tools.pagedata.PageData;
//import com.plian.system.config.YthTaskObserver;
//import com.plian.system.constant.sys.FormStatusConstant;
//import com.plian.system.constant.sys.FormTypeConstant;
//import com.plian.system.entity.dj.DjPartymassServicesInfo;
//import com.plian.system.entity.sys.Org;
//import com.plian.system.mapper.dj.DjPartymassServicesInfoMapper;
//import com.plian.system.mybatis.base.BaseFlowService;
//import com.plian.system.mybatis.base.BaseServiceImpl;
//import com.plian.system.service.dj.IDjPartymassServicesInfoService;
//import com.plian.system.service.sys.code.IFormCodeService;
//import com.plian.system.service.sys.orgzation.IOrgService;
//import com.plian.system.service.sys.user.ISysUserService;
//import com.plian.system.service.wf.FormWorkflowService;
//import com.plian.system.service.yth.IPartyServiceMapService;
//import com.plian.system.vo.dj.DjPartymassServicesInfoVO;
//import com.plian.system.wrapper.dj.DjPartymassServicesInfoWrapper;
//import lombok.AllArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang.exception.ExceptionUtils;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.stereotype.Service;
//
//import java.util.*;
//import java.util.stream.Collectors;
//
//import static com.plian.system.constant.sys.FormStatusConstant.UNREVIEWED;
//
///**
// * 党群服务中心
// **/
//@Slf4j
//@Service
//@AllArgsConstructor
//public class DjPartymassServicesInfoServiceImpl extends BaseServiceImpl<DjPartymassServicesInfoMapper, DjPartymassServicesInfo> implements IDjPartymassServicesInfoService, BaseFlowService {
//
//
//    private FormWorkflowService formWorkflowService;
//
//    private IFormCodeService formCodeService;
//
//    private final DjPartymassServicesInfoWrapper djPartymassServicesInfoWrapper;
//
//    private ISysUserService userService;
//
//    private final IOrgService orgService;
//
//    private IPartyServiceMapService partyServiceMapService;
//
//
//    @Override
//    public boolean submit(DjPartymassServicesInfo djPartymassServicesInfo) {
//        if (!Optional.ofNullable(djPartymassServicesInfo.getStatus()).isPresent()) {
//            djPartymassServicesInfo.setStatus(FormStatusConstant.UNREVIEWED);
//        }
//        return saveOrUpdate(djPartymassServicesInfo);
//    }
//
//    @Override
//    public boolean saveOrUpdate(DjPartymassServicesInfo djPartymassServicesInfo) {
//        String loginName = userService.getById(TokenUtil.getTokenUserId()).getUsername();
//        boolean flag = false;
//        if (djPartymassServicesInfo.getId() == null) {
//            flag = true;
//            djPartymassServicesInfo.setCode(formCodeService.getCode(FormTypeConstant.DJ_PARTYMASS_EVALUATION));
//            djPartymassServicesInfo.setFormType(String.valueOf(FormTypeConstant.DJ_PARTYMASS_EVALUATION));
//        }
//        boolean result = super.saveOrUpdate(djPartymassServicesInfo);
//        return result;
//    }
//
//
//    @Override
//    public boolean deleteLogic(List<Long> ids) {
//        for (Long id : ids) {
//            DjPartymassServicesInfo djPartymassServicesInfo = getById(id);
//            if (djPartymassServicesInfo == null) {
//                return false;
//            } else if (djPartymassServicesInfo.getStatus() != null &&
//                    (djPartymassServicesInfo.getStatus() != FormStatusConstant.STASH && djPartymassServicesInfo.getStatus() != FormStatusConstant.UNREVIEWED)) {
//                return false;
//            }
//
//            formWorkflowService.deleteProcessInstance(String.valueOf(id));
//        }
//        boolean result = super.deleteLogic(ids);
//        return result;
//    }
//
//    @Override
//    public HashMap<String, Object> commit(String id) {
//        handleStatus(Long.valueOf(id), FormStatusConstant.FLOW);
//        //将需要提交的流程变量返回，用于工作流侧
//        return new HashMap<String, Object>();
//    }
//
//    @Override
//    public Boolean finish(String id) {
//        /**
//         * 设置审批人和审批时间
//         */
//        DjPartymassServicesInfo djPartymassServicesInfo = getById(id);
//        djPartymassServicesInfo.setApprovedUser(TokenUtil.getTokenUserId());
//        Date now = DateUtil.now();
//        djPartymassServicesInfo.setApprovedTime(now);
//        updateById(djPartymassServicesInfo);
//        return handleStatus(Long.valueOf(id), FormStatusConstant.FINISH);
//    }
//
//    @Override
//    public Boolean reject(String id, int formState, String comment) {
//        return handleStatus(Long.valueOf(id), formState);
//    }
//
//    @Override
//    public List<PageData> findByIds(List<String> formIds) {
//        List<DjPartymassServicesInfo> djPartymemberBaseinfos = list(new LambdaQueryWrapper<DjPartymassServicesInfo>()
//                .in(DjPartymassServicesInfo::getId, Func.toLongList(StringUtils.join(formIds, StringPool.COMMA))));
//        List<DjPartymassServicesInfoVO> djPartymemberBaseinfoVOList = djPartymassServicesInfoWrapper.entityToVO(djPartymemberBaseinfos);
//        List<PageData> pageDataList = new ArrayList<>();
//        Org org = orgService.getById(TokenUtil.getTokenOrgId());
//        try {
//            if (CollectionUtil.isNotEmpty(djPartymemberBaseinfoVOList)) {
//                for (DjPartymassServicesInfoVO vo : djPartymemberBaseinfoVOList) {
//                    PageData pd = new PageData();
//                    pd.put("id", vo.getId());
//                    pd.put("UNITNAME", org.getName());
//                    pd.put("APPLICANTNAME", vo.getCreateUser());
//                    pd.put("applicantId", vo.getCreateUser());
//                    pd.put("applyOrganizationId", org.getId());
//                    pd.put("formType", FormTypeConstant.DJ_PARTYMASS_EVALUATION);
//                    pd.put("orgName", org.getName());
//                    pageDataList.add(pd);
//                }
//            }
//            return pageDataList;
//        } catch (Exception e) {
//            log.error(ExceptionUtils.getFullStackTrace(e));
//        }
//        return pageDataList;
//    }
//
//    @Override
//    public List<Integer> getFormType() {
//        return Arrays.asList(FormTypeConstant.DJ_PARTYMASS_EVALUATION);
//    }
//
//    @Override
//    public void goBackToStash(String formId) {
//        handleStatus(Long.valueOf(formId), UNREVIEWED);
//    }
//
//    /**
//     * 统一处理状态机事件方法
//     *
//     * @param id           主键
//     * @param updateStatus 更新状态
//     * @return boolean
//     */
//    private Boolean handleStatus(Long id, int updateStatus) {
//        try {
//            DjPartymassServicesInfo djPartymassServicesInfo = getById(id);
//            djPartymassServicesInfo.setStatus(updateStatus);
//            updateById(djPartymassServicesInfo);
//            return true;
//        } catch (Exception e) {
//            log.error(ExceptionUtils.getFullStackTrace(e));
//            return false;
//        }
//    }
//
//    @Override
//    @YthTaskObserver
//    public boolean saveDjPartymassServicesInfoByDataSource(String date) {
//        boolean flag = true;
//        Date start = DateUtil.parse(date, DateUtil.PATTERN_DATE);
//        //获取数据
//        //List<DjPartymassServicesInfo> syncDatas = partyServiceMapService.getSyncData(start, DateUtil.plusDays(start, 1));
//        List<DjPartymassServicesInfo> syncDatas = partyServiceMapService.getSyncData(null, null);
//
//        Map<String, String> orgSourceIds = orgService.getSourceId();
//        Map<String, String> userSourceIds = userService.getSourceId();
//
//        if (CollectionUtil.isNotEmpty(syncDatas)) {
//
//            //查询数据库存在的旧数据
//            List<DjPartymassServicesInfo> allOldData = list(new LambdaQueryWrapper<DjPartymassServicesInfo>()
//                    .eq(DjPartymassServicesInfo::getDataSource, 3)
//                    .in(DjPartymassServicesInfo::getDataSourceId, syncDatas.stream().map(DjPartymassServicesInfo::getDataSourceId).collect(Collectors.toList())));
//
//            for (DjPartymassServicesInfo syncData : syncDatas) {
//                //企业
//                String attributionUnitId = orgSourceIds.get(syncData.getAttributionUnitId());
//                if (StringUtil.isNotBlank(attributionUnitId)) {
//                    syncData.setAttributionUnitId(attributionUnitId);
//                    syncData.setCreateCompanyId(attributionUnitId);
//                }
//
//                if (CollectionUtil.isNotEmpty(allOldData)) {
//                    Optional<DjPartymassServicesInfo> oldDjPartymassServicesInfoOptional = allOldData.stream().filter(e -> syncData.getDataSourceId().equals(e.getDataSourceId())).findFirst();
//                    if (oldDjPartymassServicesInfoOptional.isPresent()) {
//                        DjPartymassServicesInfo djPartymassServicesInfo = oldDjPartymassServicesInfoOptional.get();
//                        djPartymassServicesInfo.setPartymassServicesName(syncData.getPartymassServicesName());
//                        djPartymassServicesInfo.setPartymassServicesAddress(syncData.getPartymassServicesAddress());
//                        djPartymassServicesInfo.setAttributionUnitId(syncData.getAttributionUnitId());
//                        djPartymassServicesInfo.setAttributionUnitName(syncData.getAttributionUnitName());
//                        djPartymassServicesInfo.setPartymassServicesNum(syncData.getPartymassServicesNum());
//                        djPartymassServicesInfo.setContact(syncData.getContact());
//                        djPartymassServicesInfo.setPartymassServicesActivityTheme(syncData.getPartymassServicesActivityTheme());
//                        djPartymassServicesInfo.setOrganizationName(syncData.getOrganizationName());
//                        djPartymassServicesInfo.setParticipantsNum(syncData.getParticipantsNum());
//                        djPartymassServicesInfo.setPartymassServicesCenterDesp(syncData.getPartymassServicesCenterDesp());
//                        flag = updateById(djPartymassServicesInfo);
//                    } else {
//                        flag = super.saveOrUpdate(syncData);
//                    }
//                } else {
//                    flag = super.saveOrUpdate(syncData);
//                }
//            }
//        }
//        return flag;
//    }
//}
